﻿using System;
using System.Collections.Generic;
using System.Text;
using FutureOpen.Core.func.db;

using NHibernate;
//using NHibernate.Expression;
namespace FutureOpen.Core.func.Log
{
    public class LogUserAction : MyDBEntity
    {

        public LogUserAction() { }
        private readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        //用户输入的查询
        #region Query
        private delegate void AsyncQuery(int uid, string query);
        private void AsyncAddQuery(int uid, string query)
        { 
            using (ISession session = base.factory.OpenSession())
            {
                try
                {   
                    session.Save(new Logquery(uid, query, DateTime.Now));
                    session.Flush();
                }
                catch (Exception ex)
                {
                    log.Error("AsyncAddQuery function throw exception", ex);
                }
            }
        }
        public void AddQuery(int uid, string query)
        {            
            AsyncQuery ad = new AsyncQuery(AsyncAddQuery);            
            ad.BeginInvoke(uid, query, new AsyncCallback(AsyncEndQueryInvoke), ad);
        }        
        void AsyncEndQueryInvoke(IAsyncResult ar)
        {
            AsyncQuery EndL = (AsyncQuery)ar.AsyncState;
            EndL.EndInvoke(ar);
        }
        #endregion

        //用户对于搜索的结果，点击了相应的哪篇文档
        #region Doc
        private delegate void AsyncDoc(int uid, string query, int docID);
        private void AsyncAddDoc(int uid, string query, int docID)
        {
            using (ISession session = base.factory.OpenSession())
            {
                try
                {
                    session.Save(new Logdoc(uid, docID, DateTime.Now,query));
                    session.Flush();
                }
                catch (Exception ex)
                {
                    log.Error("AsyncAddDoc function throw exception", ex);
                }
            }
        }
        public void AddDoc(int uid, string query, int docID)
        {
            AsyncDoc ad = new AsyncDoc(AsyncAddDoc);
            ad.BeginInvoke(uid, query, docID, new AsyncCallback(AsyncEndDocInvoke), ad);
        }
        void AsyncEndDocInvoke(IAsyncResult ar)
        {
            AsyncDoc EndL = (AsyncDoc)ar.AsyncState;
            EndL.EndInvoke(ar);
        }
        #endregion

        //用户点击的标签
        #region Tag
        private delegate void AsyncTag(int uid, string tag);
        private void AsyncAddTag(int uid, string tag)
        {
            using (ISession session = base.factory.OpenSession())
            {
                try
                {
                    session.Save(new Logtag(uid, tag, DateTime.Now));
                    session.Flush();
                }
                catch (Exception ex)
                {
                    log.Error("AsyncAddTag function throw exception", ex);
                }
            }
        }
        public void AddTag(int uid, string tag)
        {
            AsyncTag ad = new AsyncTag(AsyncAddTag);
            ad.BeginInvoke(uid, tag, new AsyncCallback(AsyncEndTagInvoke), ad);
        }
        void AsyncEndTagInvoke(IAsyncResult ar)
        {
            AsyncTag EndL = (AsyncTag)ar.AsyncState;
            EndL.EndInvoke(ar);
        }
        #endregion


        //用户点击了组织

        #region Org
        private delegate void AsyncOrg(int uid, int org);
        private void AsyncAddOrg(int uid, int org)
        {
            using (ISession session = base.factory.OpenSession())
            {
                try
                {
                    session.Save(new Logorg(uid, org, DateTime.Now));
                    session.Flush();
                }
                catch (Exception ex)
                {
                    log.Error("AsyncAddOrg function throw exception", ex);
                }
            }
        }
        public void AddOrg(int uid, int org)
        {
            AsyncOrg ad = new AsyncOrg(AsyncAddOrg);
            ad.BeginInvoke(uid, org, new AsyncCallback(AsyncEndTagInvoke), ad);
        }
        void AsyncEndOrgInvoke(IAsyncResult ar)
        {
            AsyncOrg EndL = (AsyncOrg)ar.AsyncState;
            EndL.EndInvoke(ar);
        }
        #endregion
    }
}
